package com.whu.tcx01.hd.road;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class NodeReducer extends Reducer<Text, NodeBean, Text, OutNodeBean> {

    private OutNodeBean outV = new OutNodeBean();

    @Override
    protected void reduce(Text key, Iterable<NodeBean> values, Context context) throws IOException, InterruptedException {

        List<Road> list = new ArrayList<>();

        for (NodeBean value : values) {
            Road road = new Road();
            road.setRoadid(value.getRoadid());
            road.setStart(value.getStart());
            road.setStop(value.getStop());
            road.setDirection(value.getDirection());
            road.setDistance(value.getDistance());
            road.setPathclass(value.getPathclass());
            road.setSpeed(value.getSpeed());
            list.add(road);
            outV.setX(value.getX());
            outV.setY(value.getY());
        }
        outV.setNewNodeid(Long.parseLong(String.valueOf(key)));
        outV.setRoads(list);
        context.write(key, outV);
    }
}
